package com.zhenai.rocyuan.modules.threadupdown.client;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;

import com.zhenai.rocyuan.modules.threadupdown.BaseSocket;

public class ClientSocket {
	private String ip = BaseSocket.SERVER_HOSE;
	private int port = BaseSocket.SERVER_PORT;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private Socket socket;
	private static final Logger log = Logger.getLogger(ClientSocket.class);

	/**
	 * ClientSocket 构造方法
	 * 
	 * @param ip
	 * @param port
	 */
	public ClientSocket(String ip, int port) {
		this.ip = ip;
		this.port = port;
	}

	public ObjectOutputStream getOut() throws IOException {
		if (out == null) {
			out = new ObjectOutputStream(socket.getOutputStream());
		}
		return out;
	}

	public ObjectInputStream getIn() throws IOException {
		if (in == null) {
			in = new ObjectInputStream(socket.getInputStream());
		}
		return in;
	}

	/**
	 * 创建socket连接
	 * 
	 * @throws Exception
	 *             exception
	 */
	public void getConnect() throws UnknownHostException, IOException {
		try {
			socket = new Socket(ip, port);
			socket.setTcpNoDelay(true);
			in = new ObjectInputStream(socket.getInputStream());
			out = new ObjectOutputStream(socket.getOutputStream());
		} catch (UnknownHostException e) {
			log.error(e, e);
			throw e;
		} catch (IOException e) {
			log.error(e, e);
			if (socket != null) {
				socket.close();
			}
			throw e;
		}
	}

	/**
	 * 往服务器端发送对象
	 * 
	 * @param obj
	 * @throws IOException
	 * @throws Exception
	 */
	public void sendObject(Object obj, ObjectOutputStream out) throws IOException {
		try {
			out.writeObject(obj);
		} catch (IOException e) {
			log.error(e, e);
			if (out != null) {
				out.close();
			}
			throw e;
		}
	}

	/**
	 * 从服务器获取对象
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 * @throws Exception
	 */
	public Object getObject(ObjectInputStream ino) throws IOException, ClassNotFoundException {
		Object o = null;
		try {
			o = ino.readObject();
		} catch (IOException e) {
			log.error(e, e);
			if (ino != null) {
				ino.close();
			}
			throw e;
		} catch (ClassNotFoundException ee) {
			log.error(ee, ee);
			throw ee;
		}
		return o;
	}

	@Deprecated
	public DataInputStream getDataInputStream() throws Exception {
		DataInputStream input = null;
		try {
			input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
		} catch (Exception e) {
			log.error(e, e);
			if (input != null) {
				input.close();
			}
			throw e;
		}
		return input;
	}

	/**
	 * close Socket
	 */
	public void closeConnect() {
		try {
			if (out != null) {
				out.close();
			}
			if (in != null) {
				in.close();
			}
			if (socket != null) {
				socket.close();
			}
		} catch (Exception e) {
			log.error(e, e);
		}
	}
}
